import time

from pexpect import expect
from selenium.webdriver.common.by import By

from common.Utils import BlogDriver

#测试博客登录页面
class BlogLogin:
    url=""
    driver=""
    def __init__(self):
        self.url="http://8.137.19.140:9090/blog_login.html"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)
    #成功登录的测试用例
    def LoginSucTest(self):
        time.sleep(2)
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 对登录结果进⾏检测, 如果跳转到了博客列表⻚才算是登录成功了
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container >div.left > div > img")
        BlogDriver.getScreenShot()
        self.driver.back()

        # self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("lisi")
        # self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
        # # time.sleep(3)
        # self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        # #能够查找到博客首页用户的昵称，说明登陆成功，否则登录失败
        # # time.sleep(3)
        # self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > h3")
        # BlogDriver.getScreenShot()
        # # #返回到登陆页面
        # self.driver.back()

#异常登录的测试用例
    def LoginFailTest(self):
        # time.sleep(2)
        #若连续多次的send_keys则会出现关键词拼接，而不是替换
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
        # time.sleep(2)
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("lisi")
        #错误的密码
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123")
        # time.sleep(2)
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        time.sleep(3)
        #检查是否登录失败
        actual=self.driver.switch_to.alert.accept()
        print(actual)
        #添加屏幕截图
        BlogDriver.getScreenShot()
        #断言检测一下是否符合预期
        assert expect==actual
        self.driver.back()

# login=BlogLogin()
# login.LoginSucTest()
# login.LoginFailTest()